var vm = new Vue({
    el: "#pageBody",
    data: {
        code: null,
        colorId: null,
        product: null,
        listTable: null,
        table: null,
        products: [],
        colors: []
    },
    ready: function() {
        var _self = this;

        $.post(host + 'jobOrderController/getSelectorList', {}, function(res) {
            if (res.result) {
                vm.products = res.products;
                vm.colors = res.colors;
                layui.use("form", function() {
                    var form = layui.form;
                    form.render("select");
                });
            }
        }, "json");

        layui.use(['table', 'layer', 'form', 'laydate'], function() {
            var table = layui.table;
            layui.laydate.render({elem: '#beginTime', type: 'datetime', changed: function() {alert(111)}});
            layui.laydate.render({elem: '#endTime', type: 'datetime'});

            _self.table = table;
            layer = layui.layer;
            _self.listTable = table.render({
                id: "dataGrid",
                elem: '#dataGrid',
                height: 'full-140',
                loading: true,
                even: true,
                cols: [[
                    {type:'numbers'},
                    {title: "排版编码", field: "PBCODE", minWidth: 100},
                    {title: "材质", field: "CZNAME", minWidth: 200},
                    {title: "颜色", field: "COLOR", minWidth: 150},
                    {title: "工单号", field: "CODE", minWidth: 155},
                    {title: "形体", field: "PRODUCT", minWidth: 200},
                    {title: "形状", field: "SHAPE", minWidth: 90},
                    {title: "规格", field: "SPEC", minWidth: 100},
                    {title: "数量", field: "QTY", minWidth: 80},
                    {title: "备注", field: "DESCRIPTION", minWidth: 200},
                ]],
                method: "post",
                url: host + "jobOrderController/getJobPaibanList",
                page: true, //是否显示分页
                limits: [12, 20, 50],
                limit: 12, //每页默认显示的数量
                done: function(res, curr, count) {
                    layuiRowspan(['PBCODE'], 1);//支持数组
                }
            });
            table.on('tool(dataGrid)', function(obj) {
                if(obj.event === 'update') {
                    var i= _self.addReportWindow(table, obj);
                }
                if(obj.event === 'view') {
                    var i= _self.testView(obj);
                }
            });
            layui.form.on('select(colorId)', function(data){
                _self.datasource = data.value;
                _self.searchData();
            });
        });
    },
    methods: {
        /*条件查询*/
        searchData: function() {
            this.listTable.reload({page: {curr: 1}, where: {code: this.code, colorId: this.colorId, product: this.product}});
        },
        send: function(_self) {
            _self = _self.target ? this : _self;

            var checkStatus = _self.table.checkStatus("dataGrid");
            var data = checkStatus.data;
            if (data.length > 0) {
                var ids = [];
                var product = null, color = null;
                for (var i = 0;i < data.length;i ++) {
                    if (color == null) {
                        color = data[i].COLOR;
                        product = data[i].CZCODE;
                    } else if (color != data[i].COLOR && product != data[i].CZCODE) {
                        layer.msg("选择的辅材信息材质、颜色不一致！");
                        return;
                    }
                    ids.push(data[i].ID);
                }
                layer.confirm("确定将选中的" + data.length + "辅材进行排版？", {icon: 3, title: '提示'}, function(index) {
                    _self.$nextTick(function() {
                        _self.$http.post(host + "jobOrderController/sendOrders", {
                            id: ids.join(",")
                        },{emulateJSON: true}).then(function(res) {
                            if (res.body.result) {
                                _self.openResult(res.body.id);
                            }
                            layer.msg(res.body.msg);
                        });
                    });
                    layer.close(index);
                });
            } else {
                layer.msg("请先勾选数据");
                _self.openResult("");
            }
        }
    }
});